<?php
/**
 * Created by PhpStorm.
 * User: Aliaksei Lazerka
 * Date: 9/28/14
 * Time: 12:20 PM
 */

namespace log;

if (!defined('NYMPH_ROOT')) {
  define('NYMPH_ROOT', dirname("../../../" . __FILE__));
  require(NYMPH_ROOT . '/local/loader/kernel/classes/URLClassLoader.php');
}

/**
 * The Logger class provides functional
 * for logging
 */
abstract class Logger implements ILogger
{
  /**
   * @var string constant colors
   */
  const INFO_COLOR = "rgba(0,0,0,0.8)";
  const DEBUG_COLOR = "rgba(0,0,255,0.8)";
  const ERROR_COLOR = "rgba(255,0,0,0.8)";

  /**
   * @var string constant levels
   */

  const INFO_LEVEL = 'INFO';
  const DEBUG_LEVEL = 'DEBUG';
  const ERROR_LEVEL = 'ERROR';

  const INT_INFO_LEVEL = 3;
  const INT_ERROR_LEVEL = 2;
  const INT_DEBUG_LEVEL = 1;

  /**
   * @var LogConfig provides configuration
   * info for the current logger
   */
  private $logConfig;

  /**
   * @return LogConfig
   */
  public function getLogConfig()
  {
    return $this->logConfig;
  }

  /**
   * @param LogConfig $logConfig
   */
  public function setLogConfig($logConfig)
  {
    $this->logConfig = $logConfig;
  }

  /**
   * (PHP 5)<br/>
   * Returns string converted using pattern
   * @param string
   * @return string converted message
   */
  public function convertForLog($message)
  {
    $pattern = $this->getLogConfig()->getPattern();
    $pattern = str_ireplace('%m', $message, $pattern);
    $pattern = str_ireplace('%d', date('d/m/Y'), $pattern);
    $pattern = str_ireplace('%t', date('h:i:s'), $pattern);
    $pattern = str_ireplace('%s', $_SERVER['PHP_SELF'], $pattern);
    return $pattern;
  }

  /**
   * (PHP 5)<br/>
   * Log level as int for checking log level priorities
   * @return int log level as int
   */
  function getLogLevelAsInt()
  {
    $logLevel = $this->logConfig->getLogLevel();
    $logLevel = strtoupper($logLevel);
    switch ($logLevel) {
      case (self::INFO_LEVEL):
        return self::INT_INFO_LEVEL;
      case (self::DEBUG_LEVEL):
        return self::INT_DEBUG_LEVEL;
      case (self::ERROR_LEVEL):
        return self::INT_ERROR_LEVEL;
    }
  }
}